using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using TA.BE;
using MyGeneration.dOOdads;

namespace HRM
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            gridViewControl1.GridViewCellDeleteClick += new GridViewControl.GridViewControl.GridViewCellDeleteClickEventHandler(Delete);
            gridViewControl1.GridViewCellClick += new GridViewControl.GridViewControl.GridViewCellClickEventHandler(Edit);

            Bind();

        }

        public void Bind()
        {
            // bind data

            try
            {


                _EMPLOYEE_TYPE ems = new _EMPLOYEE_TYPE();

                if (ems.LoadAll())
                {
                    DataTable dt = ems.DefaultView.ToTable();

                    gridViewControl1.ArrayHeaderColumn = new string[] { "ID", "NAME", "DESCRIPTION" };
                    //gridViewControl1.ArrayHideColumn = new string[] { "ID", "UDDATE_BY", "UDPATE_DATE", "CRETED_BY", "CREATED_DATE" };
                    gridViewControl1.ShowEditColumn = true;
                    gridViewControl1.ShowDeleteColumn = true;

                    // how to custom control
                    gridViewControl1.DataSource = dt;

                    gridViewControl1.DataBind();

                }
            }
            catch (Exception ex)
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // add 
            _EMPLOYEE_TYPE ems = new _EMPLOYEE_TYPE();

            if (label1.Text == "")
            {
                // save
                ems.AddNew();
                ems.NAME = textBox1.Text;
                ems.DESCRIPTION = richTextBox1.Text;
                ems.Save();
                Bind();
            }
            else
            {
                // udpate
                if (ems.LoadByPrimaryKey(Convert.ToInt32(label1.Text))) {
                    ems.NAME = textBox1.Text;
                    ems.DESCRIPTION = richTextBox1.Text;
                    ems.Save();
                }
                Bind();

                // select 
                //gridViewControl1

            }

            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            object grid = gridViewControl1;                            
        }

        void Delete(object sender, EventArgs args)
        {
            string currentEmpId = ((string[])(sender))[0];
            int currentId = Convert.ToInt32(currentEmpId);

            _EMPLOYEE_TYPE em = new _EMPLOYEE_TYPE();
            if (em.LoadByPrimaryKey(currentId))
            {
                em.MarkAsDeleted();
                em.Save();
            }

            Bind();

        }

        void Edit(object sender, EventArgs args)
        {
            object s = sender;

            string currentEmpId = ((string[])(sender))[0];
            string currentEmpName = ((string[])(sender))[1];
            string currentEmpDesc = ((string[])(sender))[2];

            label1.Text = currentEmpId;
            textBox1.Text = currentEmpName;
            richTextBox1.Text = currentEmpDesc;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Bind();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            // clear
            label1.Text = "";
            textBox1.Text = "";
            richTextBox1.Text = "";

        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                _EMPLOYEE_TYPE ems = new _EMPLOYEE_TYPE();

                ems.Where.NAME.Value = "%" + textBox2.Text.Trim() + "%";
                    //"%01%";
                    //"%" + textBox1.Text + "%";
                ems.Where.NAME.Operator = WhereParameter.Operand.Like;

                if (ems.Query.Load())
                {
                    DataTable dt = ems.DefaultView.ToTable();

                    gridViewControl1.ArrayHeaderColumn = new string[] { "ID", "NAME", "DESCRIPTION" };
                    //gridViewControl1.ArrayHideColumn = new string[] { "ID", "UDDATE_BY", "UDPATE_DATE", "CRETED_BY", "CREATED_DATE" };
                    gridViewControl1.ShowEditColumn = true;
                    gridViewControl1.ShowDeleteColumn = true;

                    // how to custom control
                    gridViewControl1.DataSource = dt;

                    gridViewControl1.DataBind();

                }
            }
            catch (Exception ex)
            {

            }
        }

    }
}